package com.yisu.auction.manager.core.service.impl;


import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.AppSignGoods;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.manager.core.mapper.AppSignGoodsMapper;
import com.yisu.auction.manager.core.mapper.GoodsInfoMapper;
import com.yisu.auction.manager.core.service.IGoodsInfoService;
import com.yisu.auction.manager.domain.request.baseInfoPackage.GoodsInfoRequest;
import com.yisu.auction.manager.domain.vo.baseInfoPackageVos.GoodsExpandInfoVo;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description： 商品信息接口实现
 * createTime：2017/8/10
 */
@Service("IGoodsTypeService")
public class GoodsInfoServiceImpl extends BaseService implements IGoodsInfoService {
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private AppSignGoodsMapper appSignGoodsMapper;

    /**
     * author:赵杰
     * Business Description : 分页获取商品信息
     *
     * @param goodsInfoRequest 分页请求信息
     * @return 商品分类列表
     */
    @Override
    public EasyUIGrid<GoodsExpandInfoVo> getGoodsInfoByPage(GoodsInfoRequest goodsInfoRequest) {
        List<GoodsExpandInfoVo> goodsInfoArrays = goodsInfoMapper.getGoodsInfoByPage(goodsInfoRequest);
        EasyUIGrid<GoodsExpandInfoVo> gridData = new EasyUIGrid<>();
        gridData.setTotal(goodsInfoRequest.getTotalRecords());
        gridData.setRows(goodsInfoArrays);
        return gridData;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，获取商品信息
     *
     * @param primaryKey 主键
     * @return
     */
    @Override
    public GoodsInfo getGoodsInfo(long primaryKey) {
        return goodsInfoMapper.selectByPrimaryKey(primaryKey);
    }

    /**
     * author:赵杰
     * Business Description : 通过商品名称获取商品信息
     *
     * @param goodsName 商品名称
     * @return
     */
    @Override
    public List<GoodsInfo> getGoodsInfoByGoodsName(String goodsName) {
        return goodsInfoMapper.getGoodsInfoByGoodsName(goodsName);
    }

    /**
     * author:赵杰
     * Business Description : 获取正在售卖的商品列表
     *
     * @return
     */
    @Override
    public List<GoodsInfo> getGoodsForShowing() {
        return goodsInfoMapper.getGoodsForShowing();
    }

    /**
     * author:赵杰
     * Business Description : 编辑商品信息
     *
     * @param goodsInfo 商品信息
     * @return
     */
    @Override
    public boolean modifyGoodsInfo(GoodsInfo goodsInfo, String appSigns) {
        GoodsInfo checkModel = goodsInfoMapper.selectByPrimaryKey(goodsInfo.getId());
        if (checkModel == null) {
            goodsInfo.setCreateDate(new Date());
            goodsInfo.setIsValid(true);
            return goodsInfoMapper.insertSelective(goodsInfo) > 0;
        } else {
            GoodsInfo updateModel = new GoodsInfo();
            updateModel.setUpdateDate(new Date());
            updateModel.setGoodsTypeId(goodsInfo.getGoodsTypeId());
            updateModel.setName(goodsInfo.getName());
            updateModel.setImages(goodsInfo.getImages());
            updateModel.setSalesNo(goodsInfo.getSalesNo());
            updateModel.setSalesType(goodsInfo.getSalesType());
            updateModel.setStock(goodsInfo.getStock());
            updateModel.setTerm(goodsInfo.getTerm());
            updateModel.setWaterLevel(goodsInfo.getWaterLevel());
            updateModel.setStatus(goodsInfo.getStatus());
            updateModel.setPrice(goodsInfo.getPrice());
            updateModel.setIsTen(goodsInfo.getIsTen());
            updateModel.setIsRechargeableCard(goodsInfo.getIsRechargeableCard());
            updateModel.setSort(goodsInfo.getSort());
            updateModel.setDetail(goodsInfo.getDetail());
            updateModel.setId(goodsInfo.getId());
            if (!StringUtils.isEmpty(appSigns)) {
                List<String> appSignArray = Linq4j.asEnumerable(appSigns.split(",")).toList();
                appSignGoodsMapper.deleteByGoodsId(updateModel.getId());
                for (String signStr : appSignArray) {
                    AppSignGoods appSignGoods = new AppSignGoods();
                    appSignGoods.setAppPackageSign(signStr);
                    appSignGoods.setCreateDate(new Date());
                    appSignGoods.setGoodsId(updateModel.getId());
                    appSignGoodsMapper.insertSelective(appSignGoods);
                }
            }
            return goodsInfoMapper.updateByPrimaryKeySelective(updateModel) > 0;
        }
    }

    /**
     * author:赵杰
     * Business Description : 获取正在售卖的商品列表
     *
     * @return
     */
    @Override
    public List<GoodsInfo> getGoodsForAll() {
        return goodsInfoMapper.getGoodsForAll();
    }

    /**
     * 更新商品信息
     *
     * @param updateModel
     * @return
     */
    @Override
    public boolean updateGoodsInfo(GoodsInfo updateModel) {
        return goodsInfoMapper.updateByPrimaryKeySelective(updateModel) > 0;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，删除商品信息
     *
     * @param primaryKey 主键ID
     * @return
     */
    @Override
    public boolean deleteGoodsInfo(long primaryKey) {
        try {
            GoodsInfo goodsInfo = goodsInfoMapper.selectByPrimaryKey(primaryKey);
            if (goodsInfo != null) {
                GoodsInfo updateModel = new GoodsInfo();
                updateModel.setId(primaryKey);
                updateModel.setIsValid(false);
                goodsInfoMapper.updateByPrimaryKeySelective(updateModel);
            }
        } catch (Exception e) {
            logger.error("通过主键ID，删除商品信息异常：" + e.getMessage());
            return false;
        }
        return true;
    }
}
